package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelRoleEntity;
import com.mt.channel.mapper.ChannelRoleMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelRoleServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelRoleServiceModel.class);

    @Autowired
    private ChannelRoleMapper channelRoleMapper;

    public ServiceResult<List<ChannelRoleEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<List<ChannelRoleEntity>> result = new ServiceResult<List<ChannelRoleEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            List<ChannelRoleEntity> channelRoleEntityEntityResult = this.channelRoleMapper.selectList(params);
            result.setResult(channelRoleEntityEntityResult);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelRoleServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelRoleEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<ChannelRoleEntity> result = new ServiceResult<ChannelRoleEntity>();
        try {
            result.setResult(this.channelRoleMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelRoleServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelRoleEntity channelRoleEntity) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelRoleMapper.insert(channelRoleEntity);
            if (save > 0) {
                id = channelRoleEntity.getChannelRoleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelRoleServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelRoleEntity channelRoleEntity) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelRoleMapper.updateByPrimaryKeySelective(channelRoleEntity);
            if (save > 0) {
                id = channelRoleEntity.getChannelRoleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelRoleServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelRoleEntity channelRoleEntity) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelRoleMapper.updateByPrimaryKeySelective(channelRoleEntity);
            if (save > 0) {
                id = channelRoleEntity.getChannelRoleId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelRoleServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKey(ChannelRoleEntity channelRoleEntity) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelRoleMapper.updateByPrimaryKey(channelRoleEntity);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(ChannelRoleEntity channelRoleEntity) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelRoleMapper.updateByPrimaryKey(channelRoleEntity);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteByPrimaryKey(Integer channelRoleId) {
        Assert.notNull(this.channelRoleMapper, "Property 'channelRoleMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelRoleMapper.doUpdateByPrimaryKey(channelRoleId);
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}
